package com.zhoug.zmui.group;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

/**
 * 高度匹配内容
 * 取所有view中最大高度作为ViewPager的高度，适用于所有page页高度相差不多的时候，否则下面会有空白的部分
 * @Author 35574
 * @Date 2020/12/11
 * @Description
 */
public class ZMUIWrapContentViewPager extends ViewPager {

    public ZMUIWrapContentViewPager(@NonNull Context context) {
        super(context);
    }

    public ZMUIWrapContentViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height)
                height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }



}
